package com.butterfly.vv.settings;


import com.beem.project.beem.BeemApplication;
import com.beem.project.beem.R;

import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class MyPreferenceActivity extends PreferenceActivity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initSettings();
	}

	private void initSettings() {
		getListView().setDividerHeight(0);
		addPreferencesFromResource(R.xml.custom_preferences);
		getListView().setFooterDividersEnabled(false);
		
		PreferenceScreen preferenceScreen = getPreferenceScreen();
		
		setLayoutResource(preferenceScreen);
		
		PreferenceHead ph = new PreferenceHead(this);
		ph.setOnBackButtonClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		ph.setOrder(0);
		preferenceScreen.addPreference(ph);
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		String accountName = getPreferenceManager().getDefaultSharedPreferences(this)
			    .getString(BeemApplication.ACCOUNT_USERNAME_KEY, "");
		Log.i("VV", accountName+">>>>>");
//		    	Preference account = findPreference(BeemApplication.ACCOUNT_USERNAME_KEY);
//		    	account.setSummary(accountName);
	}
	
	private void setLayoutResource(Preference preference) {
		if (preference instanceof PreferenceScreen) {
			PreferenceScreen ps = (PreferenceScreen) preference;
			ps.setLayoutResource(R.layout.preference_screen);
			int cnt = ps.getPreferenceCount();
			for (int i = 0; i < cnt; ++i) {
				Preference p = ps.getPreference(i);
				setLayoutResource(p);
			}
		} else if (preference instanceof PreferenceCategory) {
			PreferenceCategory pc = (PreferenceCategory) preference;
			pc.setLayoutResource(R.layout.preference_category);
			int cnt = pc.getPreferenceCount();
			for (int i = 0; i < cnt; ++i) {
				Preference p = pc.getPreference(i);
				setLayoutResource(p);
			}
		} else {
			preference.setLayoutResource(R.layout.preference);
		}
	}
	
}